<?php

namespace App\Services\Sales;
use App\Exceptions\BusinessException;
use App\Common\ResponseCode;
use App\Models\Customer\Address;
use App\Models\Sales\OrderAddress;
use App\Services\BaseService;
use Carbon\Carbon;

/**
 * 订单收货地址相关业务类
 *
 * @author WangBin
 * @date 2021-09-24
 * @package App\Http\Services\Sales
 */
class OrderAddressService extends BaseService
{
    /**
     * 保存订单收货地址
     *
     * @param int $orderId
     * @param int $addressId
     * @return bool
     * @throws
     */
    public function save(int $orderId, int $addressId)
    {
        // 获取地址
        $address = Address::getById($addressId);
        if(is_null($address)) {
            throw new BusinessException(ResponseCode::ADDRESS_NOT_FOUND);
        }
        $data = [
            'order_id' => $orderId,
            'contact_name' => $address->contact_name,
            'phone' => $address->phone,
            'area' => $address->area,
            'full_address' => $address->full_address,
            'created_at' => Carbon::now()->toDateTimeString(),
            'updated_at' => Carbon::now()->toDateTimeString()
        ];
        // 保存
        return OrderAddress::create($data);
    }
}
